$(function(){
    //设置导航高亮
    navActive();

    layui.use(['element','laydate','table'],function(){
        var laydate = layui.laydate,
            table = layui.table,
            element = layui.element;

        //列表权限控制
        function hasAuth(authCode) {
            var authArray=authStr.split(",");
            if(authArray&&authArray.length>0){
                for(var i=0;i<authArray.length;i++){
                    if(authArray[i].trim()==authCode){
                        return true;
                    }
                }
            }
            return false;
        }
        //执行一个laydate实例
        laydate.render({
            elem: '#startDate'
            ,value: formatToday() //默认当前日期
        });
        laydate.render({
            elem: '#endDate'
            ,value: formatToday()
        });

        //不传参，默认查全部
        getExceptionData();

        //查询按钮
        $(".searchBtn").click(function(){
            var dataArr = $("#exceptionParams").serializeArray();
            var dataObj = {};
            $(dataArr).each(function(i,v){
                dataObj[v.name] = v.value;
            });
            getExceptionData(dataObj);
            return false; //阻止表单默认刷新跳转
        });



        //导出模板;导出EXCEL
        $("body").on("click",".exportBtnEx",function(){
            var form = $("<form></form>");
            form.appendTo("body");
            form.attr("action", ctx+"/MonitorException/exportExcel.do");
            form.attr("method", "post");
            form.submit();
            return false;
        });

        //获取数据
        function getExceptionData(dataObj){
            $.ajax({
                type: "GET",
                url: ctx + "/MonitorException/selectAll.do",
                data: dataObj || {},  //不传参默认空对象
                dataType: "json",
                success: function(result){
                    //var data = result.data;
                    var colsArr = [];

                    var flag = hasAuth("system_auth");
                    if(flag){
                        colsArr = [ //展示的列
                            {field:'exceptionTime', title: '日期'},
                            {field:'anotherName', title: '水表位置'},
                            //{field:'dayOrNight', title: '班次'},
                            {field:'title', title: '异常标题'},
                            {field:'content', title: '异常内容'},
                            {field:'exData', title: '异常数据'},
                            {field:'remark', title: '备注'}
                        ]
                    }else{
                        colsArr = [ //展示的列
                            {field:'exceptionTime', title: '日期'},
                            {field:'anotherName', title: '水表位置'},
                            //{field:'dayOrNight', title: '班次'},
                            {field:'title', title: '异常标题'},
                            {field:'content', title: '异常内容'},
                            {field:'exData', title: '异常数据'},
                            {field:'remark', title: '备注'}
                        ]
                    }
                    exceptionList(result,colsArr);
                }
            });
        }

        //渲染表格
        function exceptionList(data,colsArr){
            table.render({
                elem: '#exception-list',
                id:'exception-list',
                data: data,
                title: '异常信息表',
                cols: [colsArr],
                page: {
                    layout: ['prev', 'page', 'next', 'count','skip','limit']
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: '首页' //显示首页
                    ,last: '尾页' //显示尾页
                    ,limit: 15  //每页显示条数
                    ,limits : [15,30,45,60]
                },
                loading: true
            });
        }

        element.on('tab(tabchange)', function (data) {
            // if(data.index == 0) {
            //     table.reload('devices');
            // }
            // else
                if(data.index == 1) {
                getExceptionData();
            }
        });
    });
});


